package top.yueyazhui.vhr.controller.Sys.SysBasic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yueyazhui.vhr.bean.Response;
import top.yueyazhui.vhr.model.Menu;
import top.yueyazhui.vhr.model.Role;
import top.yueyazhui.vhr.service.MenuService;
import top.yueyazhui.vhr.service.MenuRoleService;
import top.yueyazhui.vhr.service.RoleService;

import java.util.List;

@RestController
@RequestMapping("/Sys/SysBasic/Permission")
@Api(tags = "权限")
public class PermissionController {

    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    MenuRoleService menuRoleService;

    @GetMapping("/role")
    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    public List<Role> getAllRole() {
        return roleService.getAll();
    }

    @GetMapping("/menu")
    @ApiOperation(value = "查询所有资源", notes = "查询所有资源")
    public List<Menu> getAllMenu() {
        return menuService.getAll();
    }

    @GetMapping("/menu_role/mid/{rid}")
    @ApiOperation(value = "查询所有资源", notes = "查询所有资源")
    public List<Integer> getMIdsByRId(@PathVariable Integer rid) {
        return menuRoleService.getMIdsByRId(rid);
    }

    @PutMapping("/menu_role")
    @ApiOperation(value = "编辑资源角色", notes = "根据角色ID和资源ID编辑资源角色")
    public Response updateMenuRole(Integer rid, Integer[] mids) {
        if(menuRoleService.update(rid, mids)) {
            return Response.success("更新成功");
        }
        return Response.error("更新失败");
    }

    @PostMapping("/role")
    @ApiOperation(value = "创建角色", notes = "创建角色")
    public Response insertRole(@RequestBody Role role) {
        if(roleService.insert(role) == 1) {
            return Response.success("添加成功！");
        }
        return Response.error("添加失败！");
    }

    @DeleteMapping("/role/{id}")
    @ApiOperation(value = "删除角色", notes = "根据角色ID删除角色")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    public Response deleteRole(@PathVariable Integer id) {
        if(roleService.delete(id) == 1) {
            return Response.success("删除成功！");
        }
        return Response.error("删除失败！");
    }
}
